/*
* 表单提交验证
* */
var urlUAA="";

$(function () {
    //  在ajax启动    初始模态框
    // initRole();
})

// 设置模态框 监听事件
$("#myModal").on('hide.bs.modal', function () {
    alert( '模态隐藏');
})

function initRole() {


    // 本来是通过 ajax 请求获取的，为了给大家延时 就省略ajax  直接解析了json
    $.ajax({
        url:"/GetRoleAllServlet",
        type:'post',
        dataType:'json',
        success:function (result) {
            var res = result.data;
            for (var i = 0; i < res.length; i++) {
                var opt = $("<option value='" + res[i].id + "'>" + res[i].roleName + "</option>");
                $("#roleId").append(opt);
            }
        }
    })

    // var res = JSON.parse("[{\"id\":1,\"roleName\":\"管理员\",\"grade\":1,\"description\":\"管理员\",\"status\":1},{\"id\":2,\"roleName\":\"吧台\",\"grade\":2,\"description\":\"小卖部\",\"status\":1},{\"id\":3,\"roleName\":\"前台\",\"grade\":3,\"description\":\"前台\",\"status\":1},{\"id\":4,\"roleName\":\"收银员\",\"grade\":4,\"description\":\"收银员\",\"status\":1},{\"id\":5,\"roleName\":\"店长\",\"grade\":5,\"description\":\"店长\",\"status\":1},{\"id\":7,\"roleName\":\"新增职位\",\"description\":\"新增的职位\",\"status\":1},{\"id\":8,\"roleName\":\"保洁\",\"description\":\"保洁--负责用泳池消毒\",\"status\":1}]");


}


function addRole() {

    $("#myModalLabel").text("添加职位");
    // 调用显示模态框
    $("#myModal").modal('show');
    $("#roleForm")[0].reset();
    urlUAA= "/AddRoleServlet";



}



function updateRole(row) {

    $("#myModalLabel").text("修改职位");
    $("#roleName").val(row.roleName);
    $("#description").val(row.description);
    $("#status").val(row.status);
    $("#roleId").val(row.id);

    urlUAA= "/UpdateRoleServlet";
    // 调用显示模态框
    $("#myModal").modal('show');

}


function aaa (){
    // 当点击提交校验输入框

    var bootstrapValidator = $("#roleForm").data('bootstrapValidator');
    bootstrapValidator.validate();
    if (bootstrapValidator.isValid()){
        alert("校验成功");
        $.ajax({
            url:urlUAA,
            type:'post',
            data:$("#roleForm").serialize(),
            dataType:'json',
            success:function (result) {
                if (result.status > 0) {
                    toastr['success']("操作成功");
                    $("#myModal").modal('hide');
                    $("#roleList").bootstrapTable('refresh');
                } else {
                    toastr['error']("操作失败");
                }
            }
        })
    }else {
        alert("校验失败");



        // $("#oderForm")[0].reset();

    }
}

/**
 * 关闭模态框
 */
$("#myModal").on('hide.bs.modal', function () {
    //移除上次的校验配置
    $("#roleForm").data('bootstrapValidator').resetForm();
    $("#roleForm")[0].reset();
})

/*
* 初始化表单验证
* */
$("#roleForm").bootstrapValidator({
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        roleName: {
            validators: {
                notEmpty: {
                    message: "职位名字不能为空"
                }
            }
        },
        description: {
            validators: {
                notEmpty: {
                    message: "职位描述不能为空"
                }
            }
        },
        status: {
            validators: {
                notEmpty: {
                    message: "职位状态不能为空"
                }
            }
        }
    }

});
